package com.example.mvpdemo.simple2;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.mvpdemo.R;

import androidx.appcompat.app.AppCompatActivity;

public class Simple2Activity extends AppCompatActivity implements WeatherView_2 {

    private TextView mWeatherContent;
    private EditText mCitynameEdit;
    String cityname = "北京";
    WeatherPresenter_2 weatherPresenter_2;

    /**
     * simple2
     * mvp
     * 优化：
     * 退出activity后，Ui层引用不再被p层持有
     * 发现问题：
     * 一个类还好，如果是多个类。需要反复定义绑定和解绑
     * 解决办法：
     * 将绑定和解绑进行抽象->BasePresenter  ——>
     * Simple3中进行改进
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple2);


        mWeatherContent = findViewById(R.id.weather_content_tv);
        mCitynameEdit = findViewById(R.id.cityname_edit);
        mCitynameEdit.setText(cityname);

    }


    public void getWeatherData(View view) {

        cityname = mCitynameEdit.getText().toString().trim();
        weatherPresenter_2 = new WeatherPresenter_2(this);
        weatherPresenter_2.attachView(this);
        weatherPresenter_2.weeather(cityname);

    }

    @Override
    public void onWeatherGetSuccess(String result) {
        mWeatherContent.setText("查询结果：\n" + result);
    }

    @Override
    public void onWeatherGetFail(String result) {
        mWeatherContent.setText("查询结果：\n" + result);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (weatherPresenter_2 != null) {
            weatherPresenter_2.detachView();
        }
    }

}